package com.lgy.controller;

import com.lgy.beans.ProductCategory;
import com.lgy.beans.ProductInfo;
import com.lgy.exception.SellException;
import com.lgy.form.ProductForm;
import com.lgy.service.ProductCategoryService;
import com.lgy.service.ProductService;
import com.lgy.utils.CopyUtils;
import com.lgy.utils.IdUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("seller/product")
public class SellerProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 商品列表
     * @param page
     * @param size
     * @param map
     * @return
     * @throws SellException
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                             @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
                             Map<String, Object> map) throws SellException {
        Page<ProductInfo> productInfoPage = productService.findAll(PageRequest.of(page - 1, size));
        map.put("productInfoPage", productInfoPage);
        map.put("currentPage", page);
        map.put("size", size);
        return new ModelAndView("product/list", map);
    }

    /**
     * 商品上架
     * @param productId
     * @return
     * @throws SellException
     */
    @RequestMapping("/onSale")
    public ModelAndView onSale(@RequestParam(value = "productId") String productId) throws SellException {
        productService.onSale(productId);
        return new ModelAndView("common/success");
    }

    /**
     * 商品下架
     * @param productId
     * @return
     * @throws SellException
     */
    @RequestMapping("/offSale")
    public ModelAndView offSale(@RequestParam(value = "productId") String productId) throws SellException {
        productService.offSale(productId);
        return new ModelAndView("common/success");
    }


    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId", required = false) String productId,
                      Map<String, Object> map) throws SellException {
        if(!StringUtils.isEmpty(productId)) {
            ProductInfo productInfo = productService.findOne(productId);
            map.put("productInfo", productInfo);
        }
        // 查询所有的类目
        List<ProductCategory> productCategoryList = productCategoryService.findAll();
        map.put("categoryList", productCategoryList);
        return new ModelAndView("product/index", map);
    }

    /**
     * 保存更新
     * @param productForm
     * @return
     */
    @PostMapping("/save")
    // @CacheEvict(cacheNames = "product", key = "123", beforeInvocation = true)
    public ModelAndView save(@Valid ProductForm productForm) throws SellException {
        ProductInfo productInfo = new ProductInfo();
        if(!StringUtils.isEmpty(productForm.getProductId())) {
            productInfo = productService.findOne(productForm.getProductId());
        } else {
            productForm.setProductId(IdUtils.getId());
        }
        BeanUtils.copyProperties(productForm, productInfo, CopyUtils.getNullPropertyNames(productForm));
        productService.save(productInfo);
        return new ModelAndView("common/success");
    }
}
